import 'package:my_shop/util/toast.dart';

typedef ErrorCodeCallback = void Function(int code);
typedef PagingInterfaceCallback = void Function(bool hasMore);

class ApiError {
  // 处理接口请求错误
  static handleErr(int code, String desc, String path,
      {ErrorCodeCallback? onGetCodeErrorCallBack}) {
    desc = desc == '' ? getMsg(code) ?? '' : desc;
    if (code == 401) {
      /// 跳转到登录页面
      Toast.show(desc);
    } else if (onGetCodeErrorCallBack != null) {
      onGetCodeErrorCallBack.call(code);
    } else {
      Toast.show(desc);
    }
  }

  static String? getMsg(int code) {
    return _errorMap[code];
  }

  static const _errorMap = <int, String>{
    0: '请求成功',
    400: '请求参数不正确',
    401: '授权未登录',
    403: '没有操作权限',
    404: '请求未找到',
    405: '请求方法不正确',
    423: '请求失败，请稍后重试',
    429: '请求过于频繁，请稍后重试',
    500: '系统异常',
    501: '功能未实现',
    502: '错误的配置项',
    900: '重复请求，请稍后重试',
    901: '演示模式，禁止写操作',
    999: '未知错误',
  };
}
